package br.com.JZSolution.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import br.com.JZSolution.Estoque;
import br.com.JZSolution.modelo.Perfil;

public class PainelDeLogin extends JPanel {
	
	private JLabel loginLabel;
	private JLabel senhaLabel;
	private JTextField login;
	private JPasswordField senha;
	private JButton atualizar;
	private JButton cadastrar;
	
	public PainelDeLogin(){
		super(new BorderLayout());
		this.setPreferredSize(new Dimension(300,200));
		this.setBorder(BorderFactory.createEtchedBorder());
		loginLabel = new JLabel("Login: ");
		loginLabel.setPreferredSize(new Dimension(80,30));
		login = new JTextField();
		login.setPreferredSize(new Dimension(150,30));
		JPanel panelLogin = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelLogin.add(loginLabel);
		panelLogin.add(login);
		
		senhaLabel = new JLabel("Senha: ");
		senhaLabel.setPreferredSize(new Dimension(80,30));
		senha = new JPasswordField();
		senha.setPreferredSize(new Dimension(150,30));
		JPanel panelSenha = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelSenha.add(senhaLabel);
		panelSenha.add(senha);
		
		atualizar = new JButton("Entrar");
		atualizar.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				login(login.getText(),senha.getPassword());
			}
		});
		cadastrar = new JButton("Cancelar");
		cadastrar.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Estoque.getInstance().getMainframe().fecharTelaLogin();
			}
		});
		
		JPanel panelBtns = new JPanel(new FlowLayout(FlowLayout.CENTER));
		
		panelBtns.add(atualizar);
		panelBtns.add(cadastrar);
		
		JPanel panelCampos = new JPanel(new GridLayout(3,0));
		panelCampos.add(panelLogin,0);
		panelCampos.add(panelSenha,1);
		panelCampos.add(panelBtns,2);
		panelCampos.setPreferredSize(new Dimension(260,300));
		
		this.add(panelCampos,BorderLayout.CENTER);
		
	}
	
	private void login(String login,char[] senha){
		System.out.println("Logado!!!");
		/*String resp = ControladorDeUsuario.getInstance().autenticarUsuario(login,new String(senha));
		if(!resp.equals("ok")){
			ComunicadorDeMensagens.erro(resp);
		}*/
		if(login.equals(Perfil.ADMINISTRADOR)){
			Estoque.getInstance().getMainframe().carregarTela(new PainelDeAdm());
		}else if(login.equals(Perfil.MONITOR)){
			Estoque.getInstance().getMainframe().carregarTela(new PainelDeAdm());
		}else if(login.equals(Perfil.CAIXA)){
			Estoque.getInstance().getMainframe().carregarTela(new PainelDeCaixa());
			//Estoque.getInstance().getMainframe().carregarTelaDoCaixa();
		}
	}
	

}
